package com.baizhi.controller;

import com.baizhi.entity.City;
import com.baizhi.po.CityPO;
import com.baizhi.service.CityService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RefreshScope
@RequestMapping("/cities")
@RestController
public class CityController {

    @Autowired
    private CityService cityService;

    @GetMapping
    public List<City> queryAll() {
        List<City> cities = cityService.queryAll();
        return cities;
    }

    @PostMapping
    public City add(@RequestBody City citys) {
        String name = citys.getName();
        City city = cityService.add(name);
        return city;
    }

    @HystrixCommand(fallbackMethod = "HystrixQueryById")
    @GetMapping("{id}")
    public CityPO queryById(@PathVariable("id") Long id) {
        City city = cityService.queryById(id);
        CityPO cityPO = new CityPO(city.getId(), city.getName());
        return cityPO;
    }

    public CityPO HystrixQueryById(@PathVariable("id") Long id) {
        CityPO cityPO = new CityPO();
        cityPO.setId(id);
        cityPO.setName("服务暂时不可用");
        return cityPO;
    }
}
